package com.snpic.appaw.module.insure.service.cxProvince;

import com.snpic.appaw.module.insure.controller.app.cxProvince.vo.AppCxProvincePageReqVO;
import com.snpic.appaw.module.insure.controller.app.cxProvince.vo.AppCxProvinceSaveReqVO;
import com.snpic.appaw.module.insure.service.orc.ClientOCRService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import com.snpic.appaw.module.insure.dal.dataobject.cxProvince.CxProvinceDO;
import com.snpic.appaw.framework.common.pojo.PageResult;
import com.snpic.appaw.framework.common.util.object.BeanUtils;
import com.snpic.appaw.module.insure.dal.mysql.cxProvince.CxProvinceMapper;
import static com.snpic.appaw.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.snpic.appaw.module.insure.enums.ErrorCodeConstants.*;

/**
 * 车险出单配置-市与车险省市映射 Service 实现类
 *
 * @author jijun
 */
@Service
@Validated
public class CxProvinceServiceImpl implements CxProvinceService {

    @Resource
    private CxProvinceMapper cxProvinceMapper;

    @Resource
    private ClientOCRService clientOCRService;

    @Override
    public Long createCxProvince(AppCxProvinceSaveReqVO createReqVO) {
        // 插入
        CxProvinceDO cxProvince = BeanUtils.toBean(createReqVO, CxProvinceDO.class);
        cxProvinceMapper.insert(cxProvince);

        // 返回
        return cxProvince.getId();
    }

    @Override
    public void updateCxProvince(AppCxProvinceSaveReqVO updateReqVO) {
        // 校验存在
        validateCxProvinceExists(updateReqVO.getId());
        // 更新
        CxProvinceDO updateObj = BeanUtils.toBean(updateReqVO, CxProvinceDO.class);
        cxProvinceMapper.updateById(updateObj);
    }

    @Override
    public void deleteCxProvince(Long id) {
        // 校验存在
        validateCxProvinceExists(id);
        // 删除
        cxProvinceMapper.deleteById(id);
    }

    @Override
        public void deleteCxProvinceListByIds(List<Long> ids) {
        // 删除
        cxProvinceMapper.deleteByIds(ids);
        }


    private void validateCxProvinceExists(Long id) {
        if (cxProvinceMapper.selectById(id) == null) {
            throw exception(CX_PROVINCE_NOT_EXISTS);
        }
    }

    @Override
    public CxProvinceDO getCxProvince(Long id) {
        return cxProvinceMapper.selectById(id);
    }

    /**
     * 部分也，查询数据表cd_porvince的数据
     * @param pageReqVO 分页查询
     * @return
     */
    @Override
    public PageResult<CxProvinceDO> getCxProvincePage(AppCxProvincePageReqVO pageReqVO) {
        return cxProvinceMapper.selectPage(pageReqVO);
    }

    /**
     * 根据车牌号码查询车险出单配置-市与车险省市映射
     *
     * @param plateNo
     * @return
     */
    @Override
    public CxProvinceDO getCxProvinceByPlateNo(String plateNo) {
        CxProvinceDO cxProvinceDO = clientOCRService.getCxProvinceDO(plateNo);
        return cxProvinceDO;
    }

}